package com.gljx.web.controller;

import com.gljx.util.CommonExport;
import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.Driving;
import com.gljx.web.Entity.Paytime;
import com.gljx.web.Entity.User;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.MaintainItemService;
import com.gljx.web.service.PaytimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author lcx
 * @Date 2017/10/31 22:41
 * 基础资料->财务基础资料->收费项目
 */
@Controller
@RequestMapping("/")
public class PaytimeController {

    /*自动注入*/
    @Autowired
    private PaytimeService paytimeService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private CustomLogService customLogService;

    /**
     * @Author lcx
     * @Date 2017/11/1 11:41
     * 查询收费项目
     */
    @RequestMapping(value = "/select_pay_timet/look.action")
    public String selPaytime(HttpServletRequest request, String name
            , @RequestParam(value = "alertType", defaultValue = "") String alertType) {

        /*获取session中的登录信息*/
        User user = (User) request.getSession().getAttribute("user");
        /*查询收费项目*/
        name = name == null ? "" : name.trim();
        List<Paytime> paytimeList = paytimeService.selectPaytime(user.getFcompanyid(), name);

        request.setAttribute("paytimeList", paytimeList);
        request.setAttribute("name", name);
        request.setAttribute("alertType", alertType);

        return "backHome/basicData/financeData/paytime/paytimeList";
    }

    /**
     * @Author lcx
     * @Date 2017/11/1 11:41
     * 跳转添加收费项目窗口
     */
    @RequestMapping(value = "/go_add_pay_time")
    public String goAddPaytime(HttpServletRequest request, String alertType) throws Exception {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //获取组织
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());
        //获取上一个编码
        /*String number = customLogService.selectFunmber("ct_ds_payitem",user.getFcompanyid());*/
        //获取编码
        String fnumber = GetFnumber.fnumbers("SFXM");

        request.setAttribute("fnamel2", department.getFnameL2());
        request.setAttribute("fnumber", fnumber);

        request.setAttribute("alertType", alertType);


        return "backHome/basicData/financeData/paytime/payTimeAdd";
    }

    /**
     * @Author lcx
     * @Date 2017/11/1 13:57
     * 添加收费
     */
    @RequestMapping(value = "add_pay_time")
    public String addPaytime(HttpServletRequest request, Paytime paytime) throws Exception {
        //获取操作人信息收费项目
        User user = (User) request.getSession().getAttribute("user");
        /*获取当前时间*/
        String createTime = TimeFormat.timeFormat(new Date());
        /*获取uuid*/
        paytime.setFid(GetFid.UUIDIntoFid());
        paytime.setCfficompanyid(user.getFcompanyid());
        paytime.setFcreatorid(user.getFid());
        paytime.setFcreatetime(createTime);
        paytime.setFcontrolunitid(user.getFcompanyid());
        paytime.setCreateName(user.getName());

        paytimeService.addPayTime(paytime);

        customLogService.addLog("财务基础资料", createTime + "对收费项目进行操作", null, user.getFid());
        return "redirect:/DrivingSys/select_pay_timet/look.action?alertType=bc";
    }

    /**
     * @Author lcx
     * @Date 2017/11/2 0002 下午 4:33
     * 跳转到财务资料修改页面
     */
    @RequestMapping(value = "go_update_pay_time")
    public String goUpdatePaytime(HttpServletRequest request, String fid) throws Exception {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //把查询到的数据传给页面
        Paytime paytime = paytimeService.queryPaytimeByFid(fid);
        //获取部门
        List<Department> departments = departmentService.queryAdminByController(user.getFcompanyid());
        //获取组织
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());
        request.setAttribute("fnamel2", department.getFnameL2());
        request.setAttribute("paytime", paytime);
        request.setAttribute("dif", fid);

        return "backHome/basicData/financeData/paytime/updatePayTime";
    }

    /**
     * @Author lcx
     * @Date 2017/11/2 0002 下午 4:31
     * 对财务基础资料修改
     */
    @RequestMapping(value = "update_pay_time")
    public String updatePaytime(HttpServletRequest request, Paytime paytime) throws Exception {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //公司id
        paytime.setCfficompanyid(user.getFcompanyid());
        //控制单元id
        paytime.setFcontrolunitid(user.getFcompanyid());
        //获取当前修改时间
        String updateTime = TimeFormat.timeFormat(new Date());
        //修改时间
        paytime.setFlastupdatetime(updateTime);
        //修改人id
        paytime.setFlastupdateuserid(user.getFid());
        //修改
        paytimeService.modifyCarByFid(paytime);

        //添加日志
        customLogService.addLog("财务基础资料", TimeFormat.timeFormat(new Date()) + "对收费项目进行更新", null, user.getFid());


        return "redirect:/DrivingSys/select_pay_timet/look.action?alertType=qd";
    }

    /**
     * @Author lcx
     * @Date 2017/11/2 0002 下午 4:28
     * 对财务基础资料删除
     */
    @RequestMapping(value = "delete_pay_time")
    public String deletePaytime(HttpServletRequest request, String[] deleteId) throws Exception {

        User user = (User) request.getSession().getAttribute("user");
        //删除记录
        for (int i = 0; i < deleteId.length; i++) {
            /*过滤为空的删除操作*/
            if (!deleteId[i].equals("")) {
                paytimeService.deleteByPrimaryKey(deleteId[i]);
            }
        }
        //添加日志
        customLogService.addLog("财务基础资料", TimeFormat.timeFormat(new Date()) + "对收费项目进行删除", null, user.getFid());

        return "redirect:/DrivingSys/select_pay_timet/look.action";
    }

    /**
     * @Author lcx
     * @Date 2017/11/7 0007 上午 10:53
     * 根据名称查询
     */
    @RequestMapping("/queryPaytimeByFnumber")
    public String queryPaytimeByFnumber(HttpServletRequest request, String fnumber) {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        if ("".equals(fnumber)) {
            return "redirect:/DrivingSys/select_pay_timet/look.action";
        } else {

        }
        return "redirect:/DrivingSys/select_pay_timet/look.action";
    }

    @RequestMapping("/queryPaytimeXls")
    public String queryDrivingXls(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(defaultValue = "", value = "name") String name) {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");

        //头部数据
        List<Object> data = new ArrayList<>();
        String head[] = {"编码", "名称", "简称", "描述", "创建时间", "最后创建时间", "创建者", "修改人", "金额"
        };
        //长度
        Integer length[] = {8500, 4000, 6000, 6000, 3000, 3000, 4000, 4000, 3000};

        List<Paytime> paytimeList = paytimeService.selectPaytime(user.getFcompanyid(), name);

        for (Paytime paytime : paytimeList) {
            data.add(paytime.getFnumber());
            data.add(paytime.getFnameL2());
            data.add(paytime.getFsimplename());
            data.add(paytime.getFdescriptionL2());
            data.add(paytime.getFcreatetime());
            data.add(paytime.getFlastupdatetime());
            data.add(paytime.getCreateName());
            data.add(paytime.getUpdateName());
            data.add(paytime.getCfamount());
        }

        try {
            //导出Excel
            CommonExport.importExcel(response, "收费项目" + TimeFormat.getNowTime2() + ".xls", "收费项目", data, head, length);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/DrivingSys/select_pay_timet/look.action?state=first";
    }
}
